package com.example.user.customviewdemo.index;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * Created by user on 2016/5/19.
 * 鱼眼索引bar
 */
public class IndexSideBar extends View {

    private Paint paint;
    public static String str = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private Path path;//控件边框path
    private float singleHeight;//单个字符所占区域的高度
    private int currentIndex = -1;  //当前触碰索引位置，没有触碰时为-1
    private TextView indicatorView;//放大器view
    private OnChooseListener onChooseListener;//用于外部实现类的回调接口，便于知道本控件点击触碰的索引


    public IndexSideBar(Context context) {
        super(context);
    }

    public IndexSideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        path = new Path();
    }

    public IndexSideBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 设置放大器TextView，用于显示当前触摸字符放大后的字符
     * @param textView
     */
    public void setIndicatorView(TextView textView){
        this.indicatorView = textView;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //设置画笔属性
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setColor(Color.GREEN);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(35);

        //控件宽高
        int width = getWidth();
        int height = getHeight();

        //绘制控件边框
        path.moveTo(0,0);
        path.lineTo(width,0);
        path.lineTo(width,height);
        path.lineTo(0,height);
        path.close();
        canvas.drawPath(path,paint);


        paint.setStyle(Paint.Style.FILL);
        //计算单个字符所在区域所占的高度
        singleHeight = height/str.length();



        //开始绘制索引，索引字母为str中的字符
        Paint.FontMetrics metrics = paint.getFontMetrics();
        for (int i = 0;i<str.length();i++){
            if(i == currentIndex){
                paint.setColor(Color.RED);
            }else {
                paint.setColor(Color.GREEN);
            }
            float centerLine = singleHeight*i + singleHeight/2;
            float baseLine = centerLine - metrics.top/2 - metrics.bottom/2;
            canvas.drawText(String.valueOf(str.charAt(i)),width/2,baseLine,paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y = event.getY();
        currentIndex = getIndex(y);
        //设置当前触碰字符大放大镜和  调用外部回调接口
        if(currentIndex >= 0 && currentIndex < str.length() && indicatorView != null){
            indicatorView.setVisibility(View.VISIBLE);
            indicatorView.setText(String.valueOf(str.charAt(currentIndex)));
            if(onChooseListener != null){
                onChooseListener.onChoose(currentIndex,String.valueOf(str.charAt(currentIndex)));
            }
        }
        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
                currentIndex = -1;
                if(indicatorView != null){
                    indicatorView.setVisibility(View.GONE);
                }
                break;
        }
        invalidate();
        return true;
    }

    /**
     * 根据纵坐标y的值得到当前触点所在控件的索引位
     * @return
     */
    private int getIndex(float y){
        int index = 0;
        for(int i = 0;i<str.length();i++){
            if((i + 1)*singleHeight >= y){
                index = i;
                break;
            }
        }
        return index;
    }

    /**
     * 定义一个内部接口供外部实现，以便回调
     */
    public interface OnChooseListener{
        void onChoose(int index,String str);
    }

    /**
     * 供外部设置回调接口
     * @param onChooseListener
     */
    public void setOnChooseListener(OnChooseListener onChooseListener){
        this.onChooseListener = onChooseListener;
    }
}

